﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
using HZ.MBSM.Model;

public partial class Default3 : System.Web.UI.Page
{

    public string strRan = "";
    public string strSignData = "";
    BCACOMLib.SecurityEngineClass bca = new BCACOMLib.SecurityEngineClass();

    protected void Page_Load(object sender, System.EventArgs e)
    {
        // 在此处放置用户代码以初始化页面
        Session.Clear();
        
        this.btnUserlogin.Attributes.Add("OnClick", "javascript:return InputCheck()");
        Response.Expires = 0;
        Response.Buffer = true;
        Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
        Response.AddHeader("pragma", "no-cache");
        Response.CacheControl = "no-cache";
        if (strRan == null || strRan == "" || Session["Random"] == null || Session["Random"].ToString() == "")
        {
            //获取随机数
            strRan = bca.generateRandom(24);
            //签名随机数
            strSignData = bca.signedDataByP7(strRan);
            Session["Random"] = strRan;
        }
        if (!IsPostBack)
        {
            // 获取随机数
            strRan = bca.generateRandom(24);
            //签名随机数
            strSignData = bca.signedDataByP7(strRan);
            Session["Random"] = strRan;

            rptGgtzBind();
            rptCzbzBind();
            rptZcfgBind();
            rptJggsBind();
            rptCgdtBind();
            rptZxfbBind();
            rptQaBind();
            Picturenews_id();
        }
    }

    /// <summary>
    /// 登录按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnUserlogin_Click(object sender, EventArgs e)
    {
        try
        {
            strRan = Session["Random"].ToString();//签名随机数

            //容器名,签名需要
            string strContainerName = hddContainerName.Value;
            Session["ContainerName"] = strContainerName;

            //用户证书,验证签名需要
            string strUserCert = Request.Form.Get("UserCert");
            Session["UserCert"] = strUserCert;

            //加密证书
            string strUserEncCert = this.hddEncCert.Value;
            Session["UserEncCert"] = strUserEncCert;

            //用户签名值
            string strUserSignData = Request.Form.Get("UserSignedData");

            int rv = bca.validateCertEx(strUserCert); //验证用户证书
            //验证证书
            if (rv != 0)
            {
                return;
            }

            rv = bca.verifySignedDataByP7(strUserSignData);//验证签名
            //验证签名
            if (rv != 0)
            {
                return;
            }

            //获取唯一标示(数据库验证)
            string strUID = bca.getExtCertInfoByOID(strUserCert, "2.16.840.1.113732.2");

            if (strUID == "")
            {
                return;
            }

            string strPassword = this.txtPass.Text.Trim();//登录密码


            if (strPassword.Length == 0)
            {
                MsgBox.Aert_Alert("请填写密码！");
            }
            else
            {
                Db db = new Db();
                string sql = "select * from v_account where certid='" + strUID + "'";
                DataTable dt = db.DataTable(sql);

                if (dt.Rows.Count > 0)
                {
                    if (dt.Rows[0]["IsBid_Landing"].ToString() == "1")
                    {
                        if (SysFun.PasswordDecrypt(dt.Rows[0]["pwd_bid"].ToString()) == txtPass.Text)
                        {
                            Account Ac = new Account();
                            Ac.AccountId = Int64.Parse(dt.Rows[0]["AccountId"].ToString());
                            Ac.AccountName = dt.Rows[0]["AccountName"].ToString();
                            Ac.Pwd_Bid = dt.Rows[0]["Pwd_Bid"].ToString();
                            Ac.Pwd_Trade = dt.Rows[0]["Pwd_Trade"].ToString();
                            Ac.Type = dt.Rows[0]["Type"].ToString();
                            Ac.IsBid_Landing = dt.Rows[0]["IsBid_Landing"].ToString();
                            Ac.IsTrade_Landing = dt.Rows[0]["IsTrade_Landing"].ToString();
                            Ac.DWName = dt.Rows[0]["DWName"].ToString();
                            Ac.UserCode = dt.Rows[0]["UserCode"].ToString();
                            Ac.Sort = dt.Rows[0]["Sort"].ToString();
                            Ac.AreaId = dt.Rows[0]["AreaId"].ToString();
                            Ac.AC_SORT = dt.Rows[0]["AC_sort"].ToString();
                            Ac.CertId = dt.Rows[0]["CertId"].ToString();

                            Session["Account"] = Ac;

                            switch (Ac.Type)
                            {
                                case "0":
                                    Response.Redirect("../Manage/AccountAccredit.aspx");
                                    break;
                                case "1":
                                    Response.Redirect("../Agency/ProjectList.aspx");
                                    break;
                                case "2":
                                    if (Ac.IsTrade_Landing.Equals("0"))//关闭企业注册就按正常流程走
                                    {
                                        Response.Redirect("../Bidder/ProjectList.aspx");
                                    }
                                    else
                                    {
                                        //临时企业登录
                                        Response.Redirect("../Company/CompanyInfo.aspx");
                                    }
                                    break;
                                case "3":
                                    Response.Redirect("../Hospital/HospitalUpdate.aspx");
                                    break;
                                case "4": Response.Redirect("../Supervise/PROLIST.aspx");
                                    break;

                                case "5":
                                    Response.Redirect("../Manage/GoodsList.aspx");
                                    break;
                                case "6":
                                    Response.Redirect("../Manage/CompanyList.aspx");
                                    break;
                                case "7":
                                    Response.Redirect("../Agency/Check_BidGood_Child.aspx");
                                    break;
                                case "8":
                                    Response.Redirect("../Agency/bid_project_product_bidTable_listChild.aspx");
                                    break;
                                case "9":
                                    Response.Redirect("../Expert/ExpertYijia.aspx");
                                    break;
                                case "10":
                                    Response.Redirect("../Expert/ExpertYijia.aspx");
                                    break;
                                case "11":
                                    Response.Redirect("../Expert/CompanyYijia.aspx");
                                    break;
                                case "12":
                                    Response.Redirect("../Expert/CompanyYijia.aspx");
                                    break;
                                case "14": //配送审核

                                    Response.Redirect("../Agency/Traprojectlist.aspx");
                                    break;
                                case "15": //招标各部门
                                    Response.Redirect("../Deparment/Deparment_HomePage.aspx");
                                    break;

                                case "17": //招标各部门

                                    Response.Redirect("../Agency/company_biaoshu_dl.aspx");
                                    break;
                                case "41"://监督机构子用户

                                    Response.Redirect("../Supervise/TraManage.aspx");
                                    break;
                                default:
                                    Response.Redirect("../HomePage/Default.aspx");
                                    break;
                            }
                        }
                        else
                        {
                            MsgBox.Aert("登录密码错误！", "DefaultCA.aspx");
                        }
                    }
                    else
                    {
                        MsgBox.Aert("该用户已被停用！", "DefaultCA.aspx");
                    }
                }
                else
                {
                    MsgBox.Aert("无效的用户！","DefaultCA.aspx");
                }

                dt.Clear(); 
                dt.Dispose();
                db.Close();
                //Common_YH objYH = bll.Common_YH_LoadByCA(strUID);
                //if (objYH != null)
                //{
                //    if (objYH.Status == 0)
                //    {
                //        MessageBox.MessageAlert(Message_ID.System_012);
                //    }
                //    else if (objYH.Status == 2)
                //    {
                //        MessageBox.MessageAlert(Message_ID.System_013, objYH.CheckedDetails);
                //    }
                //    else if (objYH.Status == 3 || objYH.Status == 4)
                //    {
                //        MessageBox.MessageAlert(Message_ID.System_008);
                //    }
                //    else if (objYH.Status == 1)
                //    {
                //        string strUserType = bll.Common_YHLX_LoadByUserID(objYH.ID);
                //        //密码解密
                //        MD5 md5 = new MD5();
                //        if (md5.Encrypt(strPassword).Equals(objYH.UserPwd))
                //        {
                //            if (!WriteOperatorCookie(objYH.ID, objYH.UserName, strPassword, strProjectID, this.hidExeclVersion.Value))
                //            {
                //                MessageBox.MessageAlert(Message_ID.System_001);
                //            }
                //            else
                //            {
                //                //需要添加登录用户的类型判定
                //                if (User_Type.Admin.Equals(strUserType) || User_Type.Operator.Equals(strUserType))
                //                {
                //                    //用于区分左侧菜单
                //                    MyCookie.SetCookie("AccountType", "1", DateTime.Now.AddYears(1));
                //                    Response.Redirect("../Admin/default.aspx", false);
                //                }
                //                else
                //                {
                //                    //用于区分左侧菜单
                //                    MyCookie.SetCookie("AccountType", "2", DateTime.Now.AddYears(1));
                //                    Response.Redirect("../Enterprise/Default.aspx", false);
                //                }
                //            }
                //        }
                //        else
                //        {
                //            MessageBox.MessageAlert(Message_ID.Password_009);
                //        }
                //    }
                //    else
                //    {
                //        MessageBox.MessageAlert(Message_ID.System_009 + "  " + objYH.Status.ToString());
                //    }

                //}
                //else
                //{
                //    MessageBox.MessageAlert(Message_ID.System_006);
                //}
            }
        }
        catch (Exception ex)
        {
            MsgBox.Aert_Alert("系统发生异常！");
        }
    }

    //图片新闻id
    public string Picturenews_id()
    {
        HZ.MBSM.DBUtility.Db db = new HZ.MBSM.DBUtility.Db();
        string _strSql = "select top 4 news_id,news_title,DownLoad from dbo.newsPicture where IsShow =0 order by news_id desc ";
        System.Data.IDataReader dr = db.DataReader(_strSql);
        string _strid = "", _strtitle = "", _strDown = "";
        while (dr.Read())
        {

            _strid += "ContentShow.aspx?pid=" + MsgBox.JiaMiobj_Encrypt(dr["news_id"].ToString()) + "|";

            if (dr["news_title"].ToString().Length > 18)
            {
                _strtitle += dr["news_title"].ToString().Remove(18) + "...|";
            }
            else
            {
                _strtitle += dr["news_title"].ToString() + "|";
            }
            _strDown += "../uploadfile/" + dr["DownLoad"].ToString() + "|";

        }

        dr.Close();
        dr.Dispose();
        db.Close();
        if (_strid == "")
        {
            return "aa";
        }
        else
        {
            return _strid = _strid.Remove(_strid.Length - 1) + "∏" + _strDown.Remove(_strDown.Length - 1) + "∏" + _strtitle.Remove(_strtitle.Length - 1);
        }
    }

    /// <summary>
    /// 公告通知数据绑定
    /// </summary>
    private void rptGgtzBind()
    {
        HZ.MBSM.DBUtility.Db db = new HZ.MBSM.DBUtility.Db();

        string strSql = "select top 7 Notid,Title,AddDate,'' as img from Note order by Notid desc ";
        DataTable dt = db.DataTable(strSql);

        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (System.DateTime.Now <= Convert.ToDateTime(dt.Rows[i]["AddDate"].ToString()).AddDays(2))
                {
                    dt.Rows[i]["img"] = "&nbsp;<img src='../images/new.gif' width='30' height='10'>";
                }
            }

            rptGgtz.Visible = true;

            rptGgtz.DataSource = dt;
            rptGgtz.DataBind();
        }
        else
        {
            rptGgtz.Visible = false;
        }
        dt.Dispose();
        db.Close();
    }

    /// <summary>
    /// 操作帮助数据绑定
    /// </summary>
    private void rptCzbzBind()
    {
        HZ.MBSM.DBUtility.Db db = new HZ.MBSM.DBUtility.Db();

        string strSql = "select top 7 Id,Title,AddDate,'' as img from HelpTable  order by  id desc ";
        DataTable dt = db.DataTable(strSql);

        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (System.DateTime.Now <= Convert.ToDateTime(dt.Rows[i]["AddDate"].ToString()).AddDays(2))
                {
                    dt.Rows[i]["img"] = "&nbsp;<img src='../images/new.gif' width='30' height='10'>";
                }
            }

            rptCzbz.Visible = true;

            rptCzbz.DataSource = dt;
            rptCzbz.DataBind();
        }
        else
        {
            rptCzbz.Visible = false;
        }

        dt.Dispose();
        db.Close();
    }

    /// <summary>
    /// 政策法规数据绑定
    /// </summary>
    private void rptZcfgBind()
    {
        HZ.MBSM.DBUtility.Db db = new HZ.MBSM.DBUtility.Db();

        string strSql = "select top 6 LawId,Title,AddDate,'' as img from Law  order by  LawId desc ";
        DataTable dt = db.DataTable(strSql);

        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (System.DateTime.Now <= Convert.ToDateTime(dt.Rows[i]["AddDate"].ToString()).AddDays(2))
                {
                    dt.Rows[i]["img"] = "&nbsp;<img src='../images/new.gif' width='30' height='10'>";
                }
            }

            rptZcfg.Visible = true;

            rptZcfg.DataSource = dt;
            rptZcfg.DataBind();
        }
        else
        {
            rptZcfg.Visible = false;
        }

        dt.Dispose();
        db.Close();
    }

    /// <summary>
    /// 结果公示数据绑定
    /// </summary>
    private void rptJggsBind()
    {
        HZ.MBSM.DBUtility.Db db = new HZ.MBSM.DBUtility.Db();

        string strSql = "select top 6 ID,ShowName,ShowProject,ShowSort,ShowUrl,ShowDate,'' as img from Show order by  ShowDate desc ";
        DataTable dt = db.DataTable(strSql);

        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (System.DateTime.Now <= Convert.ToDateTime(dt.Rows[i]["ShowDate"].ToString()).AddDays(2))
                {
                    dt.Rows[i]["img"] = "&nbsp;<img src='../images/new.gif' width='30' height='10'>";
                }
            }

            rptJggs.Visible = true;

            rptJggs.DataSource = dt;
            rptJggs.DataBind();
        }
        else
        {
            rptJggs.Visible = false;
        }

        dt.Dispose();
        db.Close();
    }

    /// <summary>
    /// 采购动态数据绑定
    /// </summary>
    private void rptCgdtBind()
    {
        HZ.MBSM.DBUtility.Db db = new HZ.MBSM.DBUtility.Db();

        string strSql = "select top 6 TrendID,Title,AddDate,'' as img from GoodsTrend order by  TrendID desc ";
        DataTable dt = db.DataTable(strSql);

        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (System.DateTime.Now <= Convert.ToDateTime(dt.Rows[i]["AddDate"].ToString()).AddDays(2))
                {
                    dt.Rows[i]["img"] = "&nbsp;<img src='../images/new.gif' width='30' height='10'>";
                }
            }

            rptCgdt.Visible = true;

            rptCgdt.DataSource = dt;
            rptCgdt.DataBind();
        }
        else
        {
            rptCgdt.Visible = false;
        }

        dt.Dispose();
        db.Close();
    }

    /// <summary>
    /// 问题反馈数据绑定
    /// </summary>
    private void rptQaBind()
    {
        HZ.MBSM.DBUtility.Db db = new HZ.MBSM.DBUtility.Db();

        string strSql = "select top 12 id,title,createdate,'' as img from board  order by  id desc ";
        DataTable dt = db.DataTable(strSql);

        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (System.DateTime.Now <= Convert.ToDateTime(dt.Rows[i]["createdate"].ToString()).AddDays(2))
                {
                    dt.Rows[i]["img"] = "&nbsp;<img src='../images/new.gif' width='30' height='10'>";
                }
            }

            rptQa.Visible = true;

            rptQa.DataSource = dt;
            rptQa.DataBind();
        }
        else
        {
            rptQa.Visible = false;
        }

        dt.Dispose();
        db.Close();
    }

    /// <summary>
    /// 最新发布数据绑定
    /// </summary>
    private void rptZxfbBind()
    {
        HZ.MBSM.DBUtility.Db db = new HZ.MBSM.DBUtility.Db();
        string strSql = @"select top 6 *,'' as img from (
                            select top 6 'ContentShow.aspx' as ShowUrl, Notid as id,Title,AddDate,'ggtz' as type from Note order by AddDate desc
                            union
                            select top 6 'ContentShow.aspx' as ShowUrl, Id,Title,AddDate,'czbz' as type from HelpTable order by AddDate desc
                            union
                            select top 6 'ContentShow.aspx' as ShowUrl, LawId as id,Title,AddDate,'zcfg' as type from Law order by AddDate desc  
                            union
                            select top 6 ShowUrl, ShowProject as id,ShowName as title,ShowDate as adddate,'jggs' as type from Show order by AddDate desc 
                            union
                            select top 6 'ContentShow.aspx' as ShowUrl, TrendID as id,Title,AddDate,'cgdt' as type from GoodsTrend order by AddDate desc 
                            ) as AA order by adddate desc";

        DataTable dt = db.DataTable(strSql);

        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (System.DateTime.Now <= Convert.ToDateTime(dt.Rows[i]["AddDate"].ToString()).AddDays(2))
                {
                    dt.Rows[i]["img"] = "&nbsp;<img src='../images/new.gif' width='30' height='10'>";
                }
            }

            rptZxfb.Visible = true;

            rptZxfb.DataSource = dt;
            rptZxfb.DataBind();
        }
        else
        {
            rptZxfb.Visible = false;
        }

        dt.Dispose();
        db.Close();
    }

    #region
    /// <summary>
    /// 截取标题方法
    /// </summary>
    /// <param name="title"></param>
    /// <returns></returns>
    public string getTitle(object title)
    {
        if (title.ToString().Length > 10)
        {
            title = title.ToString().Remove(10) + "...";
        }
        return title.ToString();
    }
    public string getTitle2(object title)
    {
        if (title.ToString().Length > 11)
        {
            title = title.ToString().Remove(11) + "...";
        }
        return title.ToString();
    }
    public string getTitle3(object title)
    {
        if (title.ToString().Length > 8)
        {
            title = title.ToString().Remove(8) + "...";
        }
        return title.ToString();
    }

    /// <summary>
    /// 加密页面传值id方法
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public string getId(object id)
    {
        return MsgBox.JiaMiobj_Encrypt(id.ToString());
    }
    #endregion

}
